package com.ecoolex.ying.space.admin.graphql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.MarktingWholeHouseCase;
import com.ecoolex.ying.space.entity.WholeHouseCase;
import com.ecoolex.ying.space.pojo.AddMarktingWholeHouseCaseDTO;
import com.ecoolex.ying.space.ws.WholeHouseCaseService;

@GraphQL2Module(type = Type.Admin, description = "整屋案例营销管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = MarktingWholeHouseCaseGraphql.AUTH, name = "整屋案例营销管理")
public class MarktingWholeHouseCaseGraphql {
	
	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-MARKTINGWHOLEHOUSECASE";
	
	@Autowired
	private WholeHouseCaseService wholeHouseCaseService;
	
	@TypeField(type = MarktingWholeHouseCase.class, field = "wholeHouseCase", description = "整屋案例")
	public WholeHouseCase wholeHouseCase(@Source MarktingWholeHouseCase source){
		return wholeHouseCaseService.getWholeHouseCaseById(source.getCaseId());
	}
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	@QueryField(field = "marktingWholeHouseCase", description = "整屋案例营销详情")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "整屋案例营销详情")
	public MarktingWholeHouseCase get(@NonNull Integer id, AdminMemberAuthority user) {
		user.assertLogin();
		return wholeHouseCaseService.getMarktingWholeHouseCaseById(id);
	}
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@QueryField(field = "marktingWholeHouseCaseList", description = "整屋案例营销列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "整屋案例营销列表")
	public Pagination<MarktingWholeHouseCase> query(String name,@NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
		user.assertLogin();
		return wholeHouseCaseService.queryMarktingWholeHouseCase(name, sorts, page);
	}
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	@MutationField(field = "addMarktingWholeHouseCase", description = "关联案例")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "关联案例")
	public MarktingWholeHouseCase add(@NonNull AddMarktingWholeHouseCaseDTO add,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		WholeHouseCase wholeHouseCase = wholeHouseCaseService.getWholeHouseCaseById(add.getCaseId());
		Assert.isTrue(Check.notNull(wholeHouseCase),ResultCode.VERIFY.build(1), "案例不存在");
		return wholeHouseCaseService.addMarktingWholeHouseCase(add, user.userName());
	}
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	@MutationField(field = "deleteMarktingWholeHouseCase", description = "删除整屋案例营销")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除整屋案例营销")
	public boolean delete(@NonNull Integer id,AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		MarktingWholeHouseCase old = wholeHouseCaseService.getMarktingWholeHouseCaseById(id);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "案例营销不存在");
		wholeHouseCaseService.deleteMarktingWholeHouseCase(old, user.userName());
		return true;
	}
}
